package com.liyunc.demo.detect;

import com.liyunc.demo.detect.camera.PreviewCamera;
import com.liyunc.demo.detect.camera.PreviewCameraWithDetectAndIdentify;
import com.liyunc.demo.detect.camera.PreviewCameraWithDetectAndSave;
import com.liyunc.demo.detect.camera.RecordCameraSaveMp4;
import com.liyunc.demo.detect.recognition.TrainFromDirectory;
import com.liyunc.demo.detect.service.DetectAndRecognizeService;
import com.liyunc.demo.detect.service.DetectAndSaveService;
import com.liyunc.demo.detect.service.DetectService;
import com.liyunc.demo.detect.utils.Resources;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class Main {
    // 程序执行30秒
    private static final int TIME = 30;

    /**
     * 测试.
     */
    public static void main(String[] args) {
        // testPreviewCameraWithDetectAndSave();
        // testTrainFromDirectory();
        // testRecordCameraSaveMp4();
        // testPreviewCamera();
        testPreviewCameraWithDetectAndIdentify();
    }

    /**
     * 检测人脸，并识别，展示识别信息到预览窗口.
     */
    private static void testPreviewCameraWithDetectAndIdentify() {
        String modelFilePath = Resources.getResourceAbsolutePath(
                "data/haarcascade_frontalface_alt.xml");
//        String recognizeModelFilePath = Resources.getResourceAbsolutePath(
//                "data/faceRecognizer.xml");
        String recognizeModelFilePath = "E:\\temp\\image\\faceRecognizer.xml";

        // 这里分类编号的身份的对应关系，和之前训练时候的设定要保持一致
        Map<Integer, String> kindNameMap = new HashMap<>();
        kindNameMap.put(1, "Man");
        kindNameMap.put(2, "Women");

        // 检测服务
        DetectService detectService = new DetectAndRecognizeService(
                modelFilePath, recognizeModelFilePath, kindNameMap);

        // 开始检测
        new PreviewCameraWithDetectAndIdentify(detectService).action(TIME);
    }

    /**
     * 训练模型.
     */
    private static void testTrainFromDirectory() {
        String trainPath = "E:\\temp\\image\\";
        String outputPath = checkPath("E:\\temp\\image\\") + "faceRecognizer.xml";
        TrainFromDirectory train = new TrainFromDirectory();
        train.train(new String[] {trainPath + "man", trainPath + "man1"}, outputPath);
    }

    /**
     * 检测人脸，保存到磁盘，为学习模型提供资源.
     */
    private static void testPreviewCameraWithDetectAndSave() {
        // String modelFileUrl = "https://raw.github.com/opencv/opencv/master/data/haarcascades/haarcascade_frontalface_alt.xml";
        // 模型文件需要绝对路径
        String modelFilePath = Resources.getResourceAbsolutePath(
                "data/haarcascade_frontalface_alt.xml");
        String savePath = checkPath("E:\\temp\\image\\man\\");

        new PreviewCameraWithDetectAndSave(
                new DetectAndSaveService(
                        modelFilePath,
                        savePath
                )
        ).action(TIME);
    }

    /**
     * 摄像头抓拍视频预览.
     */
    private static void testPreviewCamera() {
        new PreviewCamera().action(TIME);
    }

    /**
     * 录制摄像头抓取的视频.
     */
    private static void testRecordCameraSaveMp4() {
        String savePath = checkPath("E:\\temp\\video\\")
                + "camera-"
                + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())
                + ".mp4";
        new RecordCameraSaveMp4(savePath).action(TIME);
    }

    /**
     * 检测路径是否存在.
     *
     * @param path 提供的路径
     * @return 处理后的路径（不存在就创建）
     */
    private static String checkPath(String path) {
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        return path;
    }
}
